//
//  CBGRequsetServers.m
//  chebabaGYS
//
//  Created by zhengminjie on 15/12/24.
//  Copyright © 2015年 zhengminjie. All rights reserved.
//

#import "CBGRequsetServers.h"
#import "CustomDefine.h"
#import "AFNetworking.h"



@implementation CBGRequsetServers

//主页显示所所有订单数据
+ (void)showAllOrderByParameters:(NSDictionary *)parameters andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    //[SXLoadingView showProgressHUD:@""];
    NSString *requestPath1 = [NSString stringWithFormat:@"http://lib1234.6655.la/API2/GrabOrder.php?action=select_order_all"];
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager POST:requestPath1 parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"dic:%@",dic);
        NSString *code=dic[@"code"];
        //NSString *message = dic[@"message"];
        
        if ([code isEqualToString:@"200"]) {
            success(dic[@"data"]);
            //[CHHLoadingView showAlertLV:message duration:0];
            
        }else{
            
            //[CHHLoadingView showAlertLV:message duration:0];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //[CHHLoadingView showAlertLV:@"发送失败" duration:0];
    }];
    
}

//获取短信验证
+ (void)getMessageCodeByParameters:(NSDictionary *)parameters andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    [SXLoadingView showProgressHUD:@""];
    NSString *requestPath = @"http://lib1234.6655.la/API/user.php?action=getSmsCode";
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager POST:requestPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"%@",dic);
        NSString *code=dic[@"code"];
        NSString *message = dic[@"message"];
        
        NSString *smscode = dic[@"data"][0][@"sms_code"];
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:smscode forKey:@"userSmscode"];
        
        if ([code isEqualToString:@"200"]) {
            
            [CHHLoadingView showAlertLV:message duration:0];
            
            //NSLog(@"验证码 === %@",code);
        }else{
            [CHHLoadingView showAlertLV:message duration:0];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [CHHLoadingView showAlertLV:@"发送失败" duration:0];
    }];
    
}

//登录
+ (void)userLoginByParameters:(NSDictionary *)parameters andSuccess:(SDSenderBlock)success andFailure:(SDSenderBlock)failure{
    
    
    [SXLoadingView showProgressHUD:@""];
    NSString *username  = parameters[@"channad_id"];
    NSString *smscode = parameters[@"user_iph"];
    //NSString *requestPath = [NSString stringWithFormat:@"http://192.168.1.104/API/user.php?action=select_iph&channad_id=%@&user_iph=%@",username,password];
    NSString *requestPath = [NSString stringWithFormat:@"http://lib1234.6655.la/API/user.php?action=select_supp&user_iphchannad_id=%@&channad_id=%@",smscode,username];
    NSLog(@"%@",parameters);
    //NSString *requestPath=@"http://192.168.1.104/API/user.php?action=select_iph&channad_id=%@&user_iph=%@",username,password";
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    
    //    [manager POST:requestPath parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //
    //        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
    //        NSLog(@"%@",dic);
    //        NSString *code=dic[@"code"];
    //        //NSString *
    //        if ([code isEqualToString:@"200"]) {
    //
    //            NSLog(@"%@",code);
    //        }else{
    //
    //            NSLog(@"%@",code);
    //        }
    //
    //    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //        NSLog(@"not ok! %@",error);
    //    }];
    
    
    [manager GET:requestPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        //NSLog(@"dic:%@",dic);
        NSString *code=dic[@"code"];
        NSString *message = dic[@"message"];
        
        if ([code isEqualToString:@"200"]) {
            success([NSString stringWithFormat:@"%@",dic[@"data"]]);
            
            NSString *userid = dic[@"data"][0][@"id"];
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            [ud setObject:userid forKey:@"userid"];
            [CHHLoadingView showAlertLV:message duration:0];
            
        }else{
            
            [CHHLoadingView showAlertLV:message duration:0];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //[SXLoadingView hideProgressHUD];
        [CHHLoadingView showAlertLV:@"登录失败" duration:0];
        
        NSLog(@"not ok! %@",error);
    }];
    
}


@end
